﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
using System.Web;
using Totoro.Core;
using Totoro.Passport;
using Totoro.ORMapping;
using NHibernate;
using NHibernate.Criterion;
using Bussiness.Interface;

namespace Bussiness
{
    /// <summary>
    /// 产品列表
    /// </summary>
    public class ProductManager 
    {
        private ProductManager()
        {

        }

        private static ProductManager instance;
        public static ProductManager Instance 
        {
            get
            {
                if (instance == null)
                {
                    instance = new ProductManager();
                    instance.LoadData();
                }
                return instance;
            }
        }
        Dictionary<string, Products> productCaches = new Dictionary<string, Products>();
        /// <summary>
        /// 加载数据
        /// </summary>
        public void LoadData()
        {
            productCaches.Clear();
            ISession session=NHHelper.GetCurrentSession();
            ICriteria crt = session.CreateCriteria<Products>();
            crt.AddOrder(new NHibernate.Criterion.Order("Price", true));//按价格的升序排列
            IList<Products> productList = null;
            productList = crt.List<Products>();
            if (productList != null)
            {
                foreach (Products product in productList)
                {
                    productCaches.Add(product.ID, product);
                }
            }
        }
        /// <summary>
        /// 获取所有的产品信息
        /// </summary>
        /// <returns>产品信息</returns>
        public Products[] GetProducts()
        {
            return productCaches.Values.ToArray();
        }
        /// <summary>
        /// 获取指定Id的产品信息
        /// </summary>
        /// <param name="id">产品Id</param>
        /// <returns>产品信息</returns>
        public Products GetProductById(string id)
        {
            if (productCaches.ContainsKey(id))
                return productCaches[id];
            return null;
        }
        /// <summary>
        /// 按价格区间对产品进行分类
        /// </summary>
        /// <param name="lowPrice">下限价格</param>
        /// <param name="upperPrice">上限价格</param>
        /// <returns>产品信息</returns>
        public Products[] GetProdcutsByPrice(decimal lowPrice, decimal upperPrice)
        {
            List<Products> products = new List<Products>();
            foreach (Products product in productCaches.Values)
            {
                if (product.Price >= lowPrice && product.Price < upperPrice)
                {
                    products.Add(product);
                }
            }
            return products.ToArray();
        }
        /// <summary>
        /// 按照价格进行排序
        /// </summary>
        /// <param name="count">返回产品信息个数</param>
        /// <param name="isAsc">是否升序</param>
        /// <returns>产品信息</returns>
        public Products[] GetProductsOrderByPrice(int count, bool isAsc)
        {
            List<Products> products = new List<Products>();
            if(count>0)
            {
                int index = 0;
                foreach(Products product in productCaches.Values)
                {
                    if (index > count)
                    {
                        break;
                    }
                    products.Add(product);
                    index++;
                }
            }
            if (!isAsc)
            {
                products.Reverse();
            }
            return products.ToArray();
        }
        /// <summary>
        /// 按商店对产品进行分类
        /// </summary>
        /// <param name="shopId">商店Id</param>
        /// <returns>产品信息</returns>
        public Products[] GetProdcutsByShop(string shopId)
        {
            List<Products> products = new List<Products>();
            foreach (Products product in productCaches.Values)
            {
                if (product.ShopID == shopId)
                {
                    products.Add(product);
                }
            }
            return products.ToArray();
        }
        /// <summary>
        /// 按产品类型对产品进行分类
        /// </summary>
        /// <param name="typeId">产品类型Id</param>
        /// <returns>产品信息</returns>
        public Products[] GetProductByType(int typeId)
        {
            List<Products> products = new List<Products>();
            foreach (Products product in productCaches.Values)
            {
                if (product.ProductTypeID == typeId)
                {
                    products.Add(product);
                }
            }
            return products.ToArray();
        }
        /// <summary>
        /// 获取大于等于开始时间的产品记录
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="count">返回记录树，若为-1，则返回全部</param>
        /// <param name="isLatest">是否为离开始时间最近的记录</param>
        /// <returns>产品信息</returns>
        public Products[] GetProductByTime(DateTime startTime,int count,bool isLatest)
        {
            List<Products> products = new List<Products>();
            foreach (Products product in productCaches.Values)
            {
                if (product.CreateTime >= startTime)
                {
                    products.Add(product);
                }
            }
            products.Sort(Comparison);
            if (!isLatest)
            {
                products.Reverse();
            }
            if (count == -1)
            {
                return products.ToArray();
            }
            return products.Take<Products>(count).ToArray();
        }
        /// <summary>
        /// 产品时间进行排序
        /// </summary>
        /// <param name="product1"></param>
        /// <param name="product2"></param>
        /// <returns></returns>
        private int Comparison(Products product1, Products product2)
        {
            return DateTime.Compare(product1.CreateTime, product2.CreateTime);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void DeleteProductByID(string id)
        {
            ISession session = NHHelper.GetCurrentSession();
            ICriteria cr = session.CreateCriteria<Products>();
            Products p = (Products)session.Load(typeof(Products), id);
            session.Delete(p);
            productCaches.Remove(id);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="p"></param>
        public void InsertProduct(Products p)
        {
            NHHelper.SaveOjb(p);
            productCaches.Add(p.ID, p);
        }

        
    }
}
